/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.breeze.generator;

import com.breeze.util.ColumnData;
import com.breeze.util.StringUtil;
import java.util.List;

/**
 *
 * @author huateng
 */
public class IbatisFileGenerator extends DefaultFileGenerator {

    @Override
    public String generate(List<ColumnData> dataList) {
        StringBuilder sb = new StringBuilder();
        String packageName = "com.breeze.entity";
        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
        sb.append("<!DOCTYPE sqlMap PUBLIC \"-//ibatis.apache.org//DTD SQL Map 2.0//EN\" \"http://ibatis.apache.org/dtd/sql-map-2.dtd\" >\r\n");
        sb.append("<sqlMap namespace=\"").append(getTableName()).append("\">\r\n");
        sb.append("\r\n");

        //commonWhere
        sb.append("<sql id=\"commonWhere\">\r\n");
        sb.append("<dynamic prepend=\"where\">\r\n");
        for (int i = 0; i < dataList.size(); i++) {
            ColumnData tmp = dataList.get(i);
            sb.append("<isNotNull property=\"").append(tmp.getHumpName()).append("\" prepend=\"and\"> ").append(tmp.getName()).append(" = #").append(tmp.getHumpName()).append("# </isNotNull>\r\n");

        }
        sb.append("</dynamic>\r\n</sql>\r\n");

        //update
//        sb.append("<update id=\"update").append(StringUtil.getString(StringUtil.getHumpString(tableName.toLowerCase()))).append("\" parameterClass=\"").append(packageName).append(".").append(StringUtil.getHumpStringString(tableName)).append(">\r\n");
//        sb.append("update "+tableName+"\r\n");
//        sb.append("<dynamic prepend=\"set\">\r\n");
//        for (int i = 0; i < dataList.size(); i++) {
//            ColumnData tmp = dataList.get(i);
//            sb.append("<isNotNull property=\"").append(tmp.getHumpName()).append("\" prepend=\",\"> ").append(tmp.getName()).append(" = #").append(tmp.getHumpName()).append("# </isNotNull>\r\n");
//        }
//        sb.append("</dynamic>\r\n");
        
        sb.append("</sqlMap>");
        return sb.toString();
    }

    public String getFileName() {
        return StringUtil.getHumpString(getTableName().toLowerCase())+".xml";
    }
}
